package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Random;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import Arguments.Arguments;

import singlethreaded.MonteCarloSimulation;
import multithreaded.MonteCarloSimulationMulti;

public class Window extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField actualPrice;
	private JTextField strikePrice;
	private JTextField interestRate;
	private JTextField time;
	private JTextField detention;
	private JTextField volatility;
	private JTextField nStep;
	private JTextField nSimulation;
	private JTextField result;
	private JButton start;
	private JLabel lblType;
	private JComboBox type;
	private JComboBox arguments;
	private JTextField resultSingle;
	private JTextField resultMulti;
	private JLabel timeSingle;
	private JLabel timeMulti;
	private JLabel timeSaved;
	private JProgressBar progressSingle;
	private JProgressBar progressMulti;

	public JTextField getResult() {
		return result;
	}

	public void setResult(String res) {
		this.result.setText(res);
	}
	
	public void setTimeSingle(String time) {
		this.timeSingle.setText(time);
	}
	
	public void setTimeMulti(String time) {
		this.timeMulti.setText(time);
	}
	
	public void incrementMulti() {
		this.progressMulti.setValue(this.progressMulti.getValue()+1);
	}
	
	public void incrementSingle() {
		this.progressSingle.setValue(this.progressSingle.getValue()+1);
	}

	public void actionPerformed(ActionEvent event) {
		  if(event.getSource() == this.start) {
			 this.resultMulti.setText("");
			 this.resultSingle.setText("");
			 this.progressMulti.setValue(0);
			 this.progressSingle.setValue(0);
			 
			Runnable run = new Runnable() {
				public void run() {
					Random numero = new Random();
					MonteCarloSimulation simulation = new MonteCarloSimulation();
					MonteCarloSimulationMulti simulationMulti = new MonteCarloSimulationMulti();
					double S = Double.parseDouble(actualPrice.getText());
					double X = Double.parseDouble(strikePrice.getText());
					double r = Double.parseDouble(interestRate.getText());
					double T = Double.parseDouble(time.getText());
					double b = Double.parseDouble(detention.getText());
					double v = Double.parseDouble(volatility.getText());
					int step = Integer.parseInt(nStep.getText());
					int simul = Integer.parseInt(nSimulation.getText());
					String cp = type.getItemAt(type.getSelectedIndex()).toString();
					if(cp == "call") {
						cp = "c";
					}else {
						cp = "p";
					}
					progressMulti.setMaximum(simul);
					progressSingle.setMaximum(simul);
					double resultat = simulation.MonteCarloStandardOption(cp, S, X, T, r, b, v, step, simul);
					resultSingle.setText(Double.toString(resultat));
					
					try {
						resultat = simulationMulti.MonteCarloStandardOption(cp, S, X, T, r, b, v, step, simul);
						resultMulti.setText(Double.toString(resultat));
						int tSaved = Integer.parseInt(timeSingle.getText()) - Integer.parseInt(timeMulti.getText());
						timeSaved.setText(Integer.toString(tSaved));
						
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
				}
			};
			
			 Thread thread = new Thread(run);
			 thread.start();
			 
		  }else if(event.getSource() == this.arguments) {
			  
			  /* RECUPERATION DES VALEURS DANS LE FICHIER CVS */
			  String[][] result = new String [9][];
			  Arguments st = new Arguments();
			  try {
				result = st.getInfos();
			  } catch (IOException e) {
				e.printStackTrace();
			  } catch (URISyntaxException e) {
				e.printStackTrace();
			  }
		
			  /* CHARGEMENT DES NOUVELLES VALEURS */
			  int index = this.arguments.getSelectedIndex();
			  switch (index) {
			  case 0:
				  /*if(result[0][0] == "c") {
					  this.type.setSelectedIndex(0);
					  this.result.setText(result[0][0]);
				  }else {
					  this.type.setSelectedIndex(1);
					  this.result.setText(result[0][0]);
				  */
				  this.actualPrice.setText(result[1][0]);
				  this.strikePrice.setText(result[2][0]);
				  this.interestRate.setText(result[3][0]);
				  this.time.setText(result[4][0]);
				  this.detention.setText(result[5][0]);
				  this.volatility.setText(result[6][0]);
				  this.nStep.setText(result[7][0]);
				  this.nSimulation.setText(result[8][0]);
				  break;
				
			  case 1:
				  /*if(result[0][1] == "c") {
					  this.type.setSelectedIndex(0);
					  this.result.setText(result[0][1]);
				  }else {
					  this.type.setSelectedIndex(1);
					  this.result.setText(result[0][1]);
				  }*/
				  this.actualPrice.setText(result[1][1]);
				  this.strikePrice.setText(result[2][1]);
				  this.interestRate.setText(result[3][1]);
				  this.time.setText(result[4][1]);
				  this.detention.setText(result[5][1]);
				  this.volatility.setText(result[6][1]);
				  this.nStep.setText(result[7][1]);
				  this.nSimulation.setText(result[8][1]);
				  break;
				  
			  case 2:
				  /*if(result[0][2] == "c") {
					  this.type.setSelectedIndex(0);
					  this.result.setText(result[0][2]);
				  }else {
					  this.type.setSelectedIndex(1);
					  this.result.setText(result[0][2]);
				  }*/
				  this.actualPrice.setText(result[1][2]);
				  this.strikePrice.setText(result[2][2]);
				  this.interestRate.setText(result[3][2]);
				  this.time.setText(result[4][2]);
				  this.detention.setText(result[5][2]);
				  this.volatility.setText(result[6][2]);
				  this.nStep.setText(result[7][2]);
				  this.nSimulation.setText(result[8][2]);
				  break;
				  
			  case 3: 
				  /*if(result[0][3] == "c") {
					  this.type.setSelectedIndex(0);
					  this.result.setText(result[0][3]);
				  }else {
					  this.type.setSelectedIndex(1);
					  this.result.setText(result[0][3]);
				  }*/
				  this.actualPrice.setText(result[1][3]);
				  this.strikePrice.setText(result[2][3]);
				  this.interestRate.setText(result[3][3]);
				  this.time.setText(result[4][3]);
				  this.detention.setText(result[5][3]);
				  this.volatility.setText(result[6][3]);
				  this.nStep.setText(result[7][3]);
				  this.nSimulation.setText(result[8][3]);
				  break;
				  
			  case 4:
				  /*if(result[0][4] == "c") {
					  this.type.setSelectedIndex(0);
					  this.result.setText(result[0][4]);
				  }else {
					  this.type.setSelectedIndex(1);
					  this.result.setText(result[0][4]);
				  }*/
				  this.actualPrice.setText(result[1][4]);
				  this.strikePrice.setText(result[2][4]);
				  this.interestRate.setText(result[3][4]);
				  this.time.setText(result[4][4]);
				  this.detention.setText(result[5][4]);
				  this.volatility.setText(result[6][4]);
				  this.nStep.setText(result[7][4]);
				  this.nSimulation.setText(result[8][4]);
				  break; 
			  }
		  }
	}
	

	public Window() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 540);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(128, 128, 128));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblMonteCarloSimulation = new JLabel("Monte Carlo Simulation");
		lblMonteCarloSimulation.setBounds(233, 6, 270, 25);
		lblMonteCarloSimulation.setFont(new Font("Helvetica", Font.BOLD, 24));
		contentPane.add(lblMonteCarloSimulation);
		
		JLabel lblPrixActuelDe = new JLabel("Prix Actuel de l'action (S)");
		lblPrixActuelDe.setBounds(59, 109, 161, 16);
		contentPane.add(lblPrixActuelDe);
		
		JLabel lblStrikePricex = new JLabel("Strike Price (X)");
		lblStrikePricex.setBounds(58, 149, 100, 16);
		contentPane.add(lblStrikePricex);
		
		JLabel lblTauxDintertDe = new JLabel("Taux d'inter\u00EAt de base (r)");
		lblTauxDintertDe.setBounds(59, 189, 161, 16);
		contentPane.add(lblTauxDintertDe);
		
		JLabel lblTempsRestant = new JLabel("Temps Restant (T)\n");
		lblTempsRestant.setBounds(59, 229, 117, 16);
		contentPane.add(lblTempsRestant);
		
		JLabel lblTauxDesFrais = new JLabel("Taux des frais de d\u00E9tention (b)");
		lblTauxDesFrais.setBounds(59, 269, 201, 16);
		contentPane.add(lblTauxDesFrais);
		
		JLabel lblVolatilitv = new JLabel("Volatilit\u00E9 (v)");
		lblVolatilitv.setBounds(59, 309, 84, 16);
		contentPane.add(lblVolatilitv);
		
		JLabel lblNstep = new JLabel("nStep");
		lblNstep.setBounds(59, 349, 61, 16);
		contentPane.add(lblNstep);
		
		JLabel lblNsimulations = new JLabel("nSimulations");
		lblNsimulations.setBounds(59, 389, 84, 16);
		contentPane.add(lblNsimulations);
		
		actualPrice = new JTextField();
		actualPrice.setBounds(282, 103, 180, 28);
		contentPane.add(actualPrice);
		actualPrice.setColumns(10);
		actualPrice.setText("40");
		
		strikePrice = new JTextField();
		strikePrice.setBounds(282, 143, 180, 28);
		contentPane.add(strikePrice);
		strikePrice.setColumns(10);
		strikePrice.setText("50");
		
		interestRate = new JTextField();
		interestRate.setBounds(282, 183, 180, 28);
		contentPane.add(interestRate);
		interestRate.setColumns(10);
		interestRate.setText("0.06");
		
		time = new JTextField();
		time.setBounds(282, 223, 180, 28);
		contentPane.add(time);
		time.setColumns(10);
		time.setText("0.5");
		
		detention = new JTextField();
		detention.setBounds(282, 263, 180, 28);
		contentPane.add(detention);
		detention.setColumns(10);
		detention.setText("0.10");
		
		volatility = new JTextField();
		volatility.setBounds(282, 303, 180, 28);
		contentPane.add(volatility);
		volatility.setColumns(10);
		volatility.setText("0.45");
		
		nStep = new JTextField();
		nStep.setBounds(282, 343, 180, 28);
		contentPane.add(nStep);
		nStep.setColumns(10);
		nStep.setText("168");
		
		nSimulation = new JTextField();
		nSimulation.setBounds(282, 383, 180, 28);
		contentPane.add(nSimulation);
		nSimulation.setColumns(10);
		nSimulation.setText("100000");
		
		start = new JButton("Lancer la simulation\n");
		start.setBounds(282, 470, 180, 29);
		start.addActionListener(this);
		contentPane.add(start);
		
		lblType = new JLabel("Type");
		lblType.setBounds(59, 69, 61, 16);
		contentPane.add(lblType);
		
		type = new JComboBox();
		type.setBounds(282, 65, 180, 27);
		type.addItem("call");
		type.addItem("put");
		type.addActionListener(this);
		contentPane.add(type);
		
		arguments = new JComboBox();
		arguments.setBounds(536, 105, 180, 27);
		arguments.addItem("set 1");
		arguments.addItem("set 2");
		arguments.addItem("set 3");
		arguments.addItem("set 4");
		arguments.addItem("set 5");
		arguments.addActionListener(this);
		contentPane.add(arguments);
		
		JLabel lblNewLabel = new JLabel("Arguments\n");
		lblNewLabel.setFont(new Font("Helvetica", Font.BOLD, 18));
		lblNewLabel.setBounds(575, 82, 105, 25);
		contentPane.add(lblNewLabel);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.LIGHT_GRAY);
		panel.setBounds(517, 149, 217, 350);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblResultat = new JLabel("Resultat");
		lblResultat.setBounds(73, 5, 71, 19);
		panel.add(lblResultat);
		lblResultat.setFont(new Font("Helvetica", Font.BOLD, 18));
		
		JLabel lblSingleThread = new JLabel("Single Thread");
		lblSingleThread.setBounds(6, 36, 85, 16);
		panel.add(lblSingleThread);
		
		resultSingle = new JTextField();
		resultSingle.setBounds(6, 64, 205, 28);
		panel.add(resultSingle);
		resultSingle.setColumns(10);
		
		progressSingle = new JProgressBar();
		progressSingle.setBounds(6, 92, 205, 20);
		panel.add(progressSingle);
		
		JLabel lblTime = new JLabel("Time : ");
		lblTime.setBounds(6, 127, 43, 16);
		panel.add(lblTime);
		
		timeSingle = new JLabel("");
		timeSingle.setBounds(61, 127, 138, 16);
		timeSingle.setText("0");
		panel.add(timeSingle);
		
		JLabel lblMultiThreads = new JLabel("Multi Threads");
		lblMultiThreads.setBounds(6, 171, 96, 16);
		panel.add(lblMultiThreads);
		
		resultMulti = new JTextField();
		resultMulti.setBounds(6, 193, 205, 28);
		panel.add(resultMulti);
		resultMulti.setColumns(10);
		
		progressMulti = new JProgressBar();
		progressMulti.setBounds(6, 222, 205, 20);
		panel.add(progressMulti);
		
		JLabel lblTime_1 = new JLabel("Time : \n");
		lblTime_1.setBounds(6, 254, 47, 16);
		panel.add(lblTime_1);
		
		timeMulti = new JLabel("\n");
		timeMulti.setBounds(53, 254, 158, 16);
		timeMulti.setText("0");
		panel.add(timeMulti);
		
		JLabel lblTimeSaved = new JLabel("Time Saved : ");
		lblTimeSaved.setBounds(6, 317, 85, 16);
		panel.add(lblTimeSaved);
		
		timeSaved = new JLabel("\n");
		timeSaved.setBounds(103, 317, 108, 16);
		timeSaved.setText("0");
		panel.add(timeSaved);
	}
}